package com.example.ccj.b.c.custom.tree;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.ccj.R;

import java.util.List;

public class TreeOldSearchDataAdapter extends ArrayAdapter<String> {
    List<String> objects;
    ISelectOldSearchTextListener selectOldSearchTextListener;
    int resource;
    public TreeOldSearchDataAdapter(@NonNull Context context, int resource, @NonNull List<String> objects,ISelectOldSearchTextListener selectOldSearchTextListener) {
        super(context, resource, objects);
        this.resource = resource;
        this.objects = objects;
        this.selectOldSearchTextListener = selectOldSearchTextListener;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        String val  = objects.get(position);
        View view = LayoutInflater.from(getContext()).inflate(resource, parent,false);
        TextView textView = view.findViewById(R.id.adapter_list_view_tree_old_search_text);
        textView.setText(val);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectOldSearchTextListener != null) selectOldSearchTextListener.result(val);
            }
        });
        return view;
    }


    @Override
    public int getCount() {
        return objects == null ? 0 : objects.size();
    }
}
